import 'package:flutter_fun_zjversion/config/storage_manager.dart';
import 'package:flutter_fun_zjversion/provider/view_state_model.dart';
import 'package:flutter_fun_zjversion/service/wan_android_repositoty.dart';

import 'user_model.dart';


const String kLoginName = 'kLoginName';
class LoginModel extends ViewStateModel{
  final UserModel userModel;

  LoginModel(this.userModel):assert(userModel != null);

  String getLoginName(){
    return StorageManager.sharedPreferences.getString(kLoginName);
  }


  Future<bool> login(loginName,password) async{
    setBusy(true);
    try{
      var user = await WanAndroidRepository.login(loginName, password);
      userModel.saveUser(user);
      StorageManager.sharedPreferences.setString(kLoginName, userModel.user.username);
      setBusy(false);
      return true;
    }catch(e){
      setError(e is Error ? e.toString() : e.message);
      return false;
    }
  }

  Future<bool> loginOut() async{
    if(!userModel.hasUser){
      return false;
    }
    setBusy(true);
    try{
      await WanAndroidRepository.loginOut();
      userModel.clearUser();
      setBusy(false);
      return true;
    }catch(e){
      setError(e is Error? e.toString() : e.message);
      return false;
    }
  }
}